import {setItem, getItem, removeItem} from '../../assets/js/storage.js'
export default {
  data () {
    return {
      thisRouter: this.tableBoxId || this.$route.meta.name, // 当前的路由标签meta
      isShowSetting: false,
      isReload: false,
      isSaveSearch: this.isSetSearch && this.isSetSearch !== 'false' ? true : false
    }
  },
  watch: {
    //监控是否有遮罩
    isMask: function (val) {
      if(this.isShowSetting && !val) {
        this.hideSettingBoxFn();
      }
    },
    isSingle: function (val) {
      this.saveOtherSetting('sin', val);
    },
    isMoreFilters: function (val) {
      this.saveOtherSetting('fil', val);
    }
  },
  created () {
    this.recoverSetting();
  },
  methods: {
    //保存除顺序外的其他设置
    saveOtherSetting(key, value) {
      if(this.isSaveSearch) {
        let name = this.thisRouter;
        let _storage = getItem(name);
        _storage = _storage ? _storage : {};
        _storage[key] = value;
        setItem(name,_storage);
      }
    },
    //保存顺序设置
    saveSetting () {
      if(this.isSaveSearch) {
        let _selectionArr = this.selectionList.slice(0);
        let name = this.thisRouter;
        let _storage = getItem(name);
        let searchList = [];
        _storage = _storage ? _storage : {};
        for(let i = 0, len = _selectionArr.length; i< len; i++) {
          searchList.push(_selectionArr[i].index);
        }
        _storage.seek = searchList;
        setItem(name,_storage);
      }
    },
    // 根据保存的设置恢复搜索栏
    recoverSetting () {
      if(this.isSaveSearch) {
        let name = this.thisRouter;
        let _storage = getItem(name);
        _storage = _storage ? _storage : {};
        if(_storage) {
          if(_storage.seek) {
            let _searchArr = _storage.seek;
            this.selectionList.sort(this.sortByIndex(_searchArr));
          }
          if(_storage.sin !== undefined) {
            this.isSingle = _storage.sin;
          }
          if(_storage.fil !== undefined) {
            this.isMoreFilters = _storage.fil;
          }
        }
      }

    },
    //根据表格头的index在保存的数组的位置排序
    sortByIndex (arr) {
      return function(a, b) {
        let Val1 = arr.indexOf(a.index);
        let Val2 = arr.indexOf(b.index);
        return Val1 - Val2;
      }
    },
    //移动项
    moveItemFn (index, isUp) {
      let _this = this;
      let _upIndex = 0;
      if(isUp && index == _upIndex) {
        this.showToast('当前已是第一列！', 0, 1000);
        return;
      }
      if(!isUp && index == this.selectionList.length - 1) {
        this.showToast('当前已是最后一列！', 0, 1000);
        return;
      }
      this.isReload = true;
      let selectionArr = this.selectionList;
      let thisItem = selectionArr.splice(index, 1);
      let _idx = isUp ? index - 1 : index + 1;
      thisItem[0].isShow = false;
      selectionArr.splice(_idx, 0, thisItem[0]);
      this.$nextTick(function(){
        this.isReload = false;
      })
    },
    //鼠标hover到表格设置行的时候
    showOperation (index) {
      this.selectionList[index].isShow = true;
    },
    //鼠标离开表格设置行的时候
    hideOperation (index) {
      this.selectionList[index].isShow = false;
    },
    //切换搜索栏的设置框的显示隐藏状态
    toggleSettingFn () {
      if(this.isShowSetting) {
        this.hideSettingBoxFn();
      } else {
        this.showSettingBoxFn();
      }
    },
    //显示表格设置框
    showSettingBoxFn () {
      this.isShowSetting = true;
      this.$store.commit('ISMASK_TOGGLE', true);
    },
    //隐藏表格设置框
    hideSettingBoxFn () {
      this.isShowSetting  = false;
      this.saveSetting();
    }
  }
}
